/*
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef VIDEODECODER_H
#define VIDEODECODER_H

#include "multimedia/player_framework/native_avcodec_videodecoder.h"
#include "multimedia/player_framework/native_avbuffer_info.h"
#include "sample_info.h"
#include "sample_callback.h"
#include "ff_ffplay_def.h"
#include "ff_ffplay.h"
#include <multimedia/player_framework/native_avcodec_videodecoder.h>
#include <multimedia/player_framework/native_avcapability.h>
#include <multimedia/player_framework/native_avcodec_base.h>
#include <multimedia/player_framework/native_avformat.h>


class VideoDecoder {
public:
    VideoDecoder() = default;
    ~VideoDecoder();

    //int32_t Create(const std::string &codecMime);
    int32_t Config(OH_AVFormat *format, struct EGLNativeWindowType *native_window);
    int32_t Start();
    int32_t PushInputData(CodecBufferInfo &info);
    int32_t FreeOutputData(uint32_t bufferIndex, bool render);
    int32_t Release();
    int32_t ReadSample(OH_AVBuffer *buffer, OH_AVCodecBufferAttr &attr);
    //std::unique_ptr<std::thread> decInputThread_ = { nullptr };
    std::unique_ptr<std::thread> decOutputThread_ = { nullptr };
    CodecUserData *decContext_ = { nullptr };
    std::atomic<bool> isStarted_ { false };
    std::condition_variable doneCond_;
    std::mutex mutex_;
    FFPlayer *ffp;
    OH_AVCodec *decoder_;
    void DecOutputThread();
    bool isPaused_ { false };
private:
    int32_t SetCallback(CodecUserData *codecUserData);

    void StartRelease();
    std::unique_ptr<VideoDecoder> videoDecoder_ = { nullptr };
    bool isAVBufferMode_ = false;

    void* nativeWindow_ { nullptr };
};

#endif // VIDEODECODER_H